package zkclient.dwj.cn;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.BytesPushThroughSerializer;
import org.I0Itec.zkclient.serialize.SerializableSerializer;

import java.util.List;

/**
 * Created by Administrator on 2017/11/13 0013.
 */
public class SubscribeDataChange {
    public static void main(String[] args) throws InterruptedException {
        ZkClient zc = new ZkClient("192.168.110.134:2181", 10000, 10000, new BytesPushThroughSerializer());//序列化器要修改
        System.out.println("connect ok");

        zc.subscribeDataChanges("/ding", new ZkDataListener());//监听节点中数据的变化
        Thread.sleep(Integer.MAX_VALUE);
    }

    private static class ZkDataListener implements IZkDataListener {


        public void handleDataChange(String s, Object o) throws Exception {//数据改变是会触发
            System.out.println(s);
            System.out.println(o.toString());
        }

        public void handleDataDeleted(String s) throws Exception {//数据删除时会触发
            System.out.println(s);
        }
    }
}
